﻿using System;
using System.ComponentModel.Composition;
using Cubicle.Core.Interfaces;

namespace Cubicle.Core.Processors.Parameter
{
    [Export("Cubicle.Processors.Parameter.OtherParameterResolver", typeof(IProcessor<ParameterRequest>))]
    [ExportMetadata("Name", "OtherParameterResolver")]
    [ExportMetadata("Description", "Forces a cast for unresolved parameters.")]
    public class OtherParameterResolver : Processor<ParameterRequest>
    {
        #region Overrides of Processor<ParameterRequest>

        public override ParameterRequest Process(ParameterRequest request)
        {
            try
            {
                var result = Convert.ChangeType(request.Source, request.TargetType);
                request.Complete(result);
            }
            catch
            {
                request.Complete(null);
            }
            return request;
        }

        #endregion
    }
}